Introduction

ggmap is a new tool which enables such visualization by combining with GoogleMaps, Openstreet … etc.

Besides, ggmap can combine with the powerful visualization packege, ggplot2, which allow us to extent the applications of geographic visualization. ex: geom_path …

Packages required

About map

About wrangling data


Let’s go

Part 1 : 在做地圖視覺化前,第一步要先學會畫地圖!

1. 載入ggmap套件

> library(ggmap)

2. qmap = get_map + ggmap

qmapget_mapggmap 的快速指令

  • get_map : 從 Google Map or Open Street Map(OSM) or ..取得地圖
  • ggmap : 將取得的地圖畫出來
  • 可以透過?get_map ?ggmap找相關的參數設定
  • location可以放經緯度or地名…
    • “政治大學”
    • c(lon = -95.3632715, lat = 29.7632836)
  • zoom 可以縮放(需要整數 1~21)
> qmap(location = "台北車站", zoom = 14, source = "google")

> qmap(location = "政治大學", zoom = 14, source = "osm")

> # it's the same
> map <- get_map(location = '台北車站',zoom = 14, source= 'google')
> ggmap(map, fullpage=T)

Part 2 : 地圖具備,只欠經緯!

1. 找一個擁有經緯度的資料集來玩玩吧!

Download Dataset from Here and load it in

> library(RCurl)
> url <- getURI('https://raw.githubusercontent.com/unityculture/20150809_GeoViz_Tutorial/master/hsinchu.csv')
> data <- read.csv(text = url)
   土地面積 建物面積 屋齡 房屋樓層 車位數目 有無管理 時間序列  單價_坪
1:    13.37 15.90847   23       16        1        1        1 163434.7
2:    26.68 43.79595    2       11        1        1        1 123517.4
3:    29.13 48.39697    2       11        1        1        1 121494.2
4:    11.08 34.20972   20       12        0        1        1 105233.1
5:    38.96 97.03595    4       14        1        1        1 252482.6
6:    27.95 49.85200    2       11        2        1        1 129173.6
        lon      lat                           road.name price.level
1: 120.9775 24.81635      新竹市新竹巿田美三街48巷1~30號      15~20W
2: 120.9395 24.79717 新竹市新竹巿牛埔南路142巷33弄1~30號      10~15W
3: 120.9395 24.79720 新竹市新竹巿牛埔南路142巷33弄1~30號      10~15W
4: 120.9599 24.79710     新竹市新竹巿中華路三段121~150號      10~15W
5: 120.9693 24.81003          新竹市新竹巿北大路91~120號        >25W
6: 120.9393 24.79739 新竹市新竹巿牛埔南路142巷33弄1~30號      10~15W

2. 呼叫地圖!

> hs.map <- qmap(location='新竹市',zoom=13,source='google')

3. 點點圖登場 ~ 將data的經緯度點上去!

  • geom_point : ggplot2中畫點圖的工具
  • ggplot2為layer的概念,地圖畫上去在疊點點上去
  • 覺得地圖中右邊的點點有一點不甘心?
  • 覺得地圖的黑點點不好看?
> hs.map + geom_point(data=data,
+                     aes(x=lon, y=lat))

  • Tips : 若同一點資料量很多時,無法看出密集度怎麼辦? -> alpha
  • 調整中心位置至馬偕紀念醫院新竹院區, 地圖調整至黑白色
> hs.map <- qmap(location='馬偕紀念醫院新竹院區',zoom=13,source='google',color='bw')
> hs.map + geom_point(data=data,
+                     aes(x=lon, y=lat, color='darkred'),alpha=0.2) +
+          guides(color='none') # 為了不要讓圖例出來

  • 剛剛只有交易房屋的所在地而已,並未加入價格資訊 -> fill
  • 透過price.level欄位將不同價位的房屋用不同顏色標出
> hs.map + geom_point(data=data,
+                     aes(x=lon, y=lat,
+                         color=price.level,fill=price.level)) 

  • 或是將價格資訊用點點大小表示!
> hs.map + geom_point(data=data,
+                     aes(x=lon, y=lat,
+                         size=price.level,color='darkred'),alpha=0.2) +
+          guides(color='none')

4. 區域密度圖 (我取的la ^ _ ^)

有時候單純想知道哪一些區域是相對密集時也可以考慮這種呈現方式

一樣可以透過顏色來區分密度、或是價格資訊

  • stat_bind呼叫區域密度圖!
  • bins為格子數目, ex: bins=10 代表將地圖切割成10x10
> hs.map +
+   stat_bin2d(
+     aes(x = lon, y = lat),
+     bins = 10, alpha = 1/2,
+     data = data)

  • 深淺代表數目,顏色代表價格
> hs.map +
+   stat_bin2d(
+     aes(x = lon, y = lat, fill=price.level),
+     bins = 30, alpha = .2,
+     data = data)

5. 熱圖 (also called Heat Map)

  • scale_fill_gradient : 自己設定熱圖顏色(低至高)!
  • scale_alpha : 自己設定熱圖透明度(低至高)!
> hs.map +
+   stat_density2d(
+     aes(x = lon, y = lat, fill = ..level..,
+       alpha = ..level..),size=.01, bins = 16, data = data,
+     geom = "polygon")+
+   scale_fill_gradient(low = "yellow", high = "red")+
+   scale_alpha(range = c(0.00, 0.25), guide = FALSE)

Reference